package osgi;

import java.util.Map;

import org.eclipse.core.runtime.adaptor.EclipseStarter;
import org.eclipse.osgi.framework.internal.core.FrameworkProperties;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.Version;
import org.osgi.framework.launch.Framework;

public class EquinoxLauncher {
	private static BundleContext context;

	public static void launch() {
		// 根据要加载的bundle组装出类似a.jar@start,b.jar@3:start这样格式的osgibundles字符串来
		String osgiBundles="";
		
		// 配置Equinox的启动 
		FrameworkProperties.setProperty("osgi.noShutdown", "true");
		FrameworkProperties.setProperty("eclipse.ignoreApp", "true");  
	    FrameworkProperties.setProperty("osgi.bundles.defaultStartLevel", "4");  
	    FrameworkProperties.setProperty("osgi.bundles", osgiBundles);  
	    // 根据需要设置bundle所在的路径  
	    String bundlePath="";  
	    // 指定要加载的plugins所在的目录  
	    FrameworkProperties.setProperty("osgi.syspath", bundlePath);  
	    // 调用EclipseStarter，完成容器的启动，指定configuration目录  
	    try {
			EclipseStarter.run(new String[]{"-configuration", "configuration", "-console"}, null);
		} catch (Exception e) {
			e.printStackTrace();
		}  
	    // 通过EclipeStarter获得BundleContext  
	    context = EclipseStarter.getSystemBundleContext();
	    System.err.println("osgi launched");
	}
	
    public static void exit() {
    	try {  
            EclipseStarter.shutdown();  
            context=null;
            System.err.println("osgi exit");
        }  
        catch (Exception e) {
            System.err.println("停止equinox容器时出现错误:" + e);  
            e.printStackTrace();  
        } 
	}
    
    
    public static Object getService(String serviceName){  
        ServiceReference<?> serviceRef = context.getServiceReference(serviceName);  
        Object obj = null;
        if(serviceRef != null) {
            obj = context.getService(serviceRef);
            context.ungetService(serviceRef);
        }

        return obj;
    } 
    
    public static Object getService(Class<?> clazz){  
        ServiceReference<?> serviceRef = context.getServiceReference(clazz);
        Object obj = null;
        if(serviceRef != null) {
        	obj = context.getService(serviceRef);
            context.ungetService(serviceRef);
        }
        return obj;  
    } 
    
    public static void getBundles() {
    	Bundle[] bundles = context.getBundles();
    	for (Bundle bundle : bundles) {
    		long id = bundle.getBundleId();
    		int state = bundle.getState();
    		String name = bundle.getSymbolicName();
    		Version version = bundle.getVersion();
    		System.out.println(id + "," + state + "," + name + "_" 
    		        + version.getMajor() + "." + version.getMinor() + "." 
    				+ version.getMicro() + "." + version.getQualifier());
    	}
    }
    
    public static Bundle getBundle(long id) {
    	return context.getBundle(id);
    }
    
    public static void stopBundle(long id) {
    	Bundle bundle = getBundle(id);
    	if (bundle != null) {
			try {
				bundle.stop();
			} catch (BundleException e) {
				e.printStackTrace();
			}
    	}
    }

	public Framework newFramework(Map<String, String> configuration) {
		// TODO Auto-generated method stub
		return null;
	}
}
